﻿using DapperDB.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaskSchedul;
using TaskSchedul.Web.Model;

namespace TaskSchedul.Web.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            List<Task_Job> joblist = new Task_JobDap().GetAll();

            ViewBag.ServiceList = joblist;

            return View();
        }

        public ActionResult NewService()
        {
            return View();
        }


        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Add(FromJob form)
        {
            string msg = string.Empty;

            var p = new { status = true, msg = "添加成功！" };

            try
            {

                Task_Job job = new Task_Job();

                job.ID = Guid.NewGuid().ToString();
                job.Job_ServiceName = form.Job_ServiceName;
                job.Job_Name = form.Job_Name;
                job.Job_Group = form.Job_Group;
                job.Trigger_Name = "Trigger" + form.Job_Name;
                job.Trigger_Group = "Trigger" + form.Job_Group;
                job.SetUpTime = form.SetUpTime;
                job.Job_DllPath = form.Job_DllPath;
                job.ProcessTime = 0;
                job.LastTime = "-";
                job.NextTime = "-";
                job.CreateTime = DateTime.Now;
                job.Job_State = -1;

                new Task_JobDap().Insert(job);

            }
            catch (Exception e)
            {
                p = new { status = false, msg = "添加失败！" };
                new Task_LogDap().Insert(new Task_Log()
                {
                    ID = Guid.NewGuid().ToString(),
                    Title = "新增服务！",
                    LogText = e.ToString(),
                    CreateTime = DateTime.Now

                });

            }
            return Json(p);
        }

        [HttpPost]

        public JsonResult SendMsg(SendMsgModel form)
        {
            string msg = string.Empty;

            var p = new { status = true, msg = "操作成功！" };

            try
            { 
                MsgModel model = new MsgModel()
                {
                    TaskID = form.taskid,
                    Operation = form.type 
                };
                RabbitMqHelper.SendMessage(ToolHelper.GetJson(model));
            }
            catch (Exception e)
            {
                p = new { status = false, msg = "操作成功！" };
                new Task_LogDap().Insert(new Task_Log()
                {
                    ID = Guid.NewGuid().ToString(),
                    Title = "发送命令！",
                    LogText = e.ToString(),
                    CreateTime = DateTime.Now

                });

            }
            return Json(p);
        }
    }
}